package com.springday3aoptransaction.service.impl;

import com.springday3aoptransaction.dao.AccountDao;
import com.springday3aoptransaction.domain.Account;
import com.springday3aoptransaction.service.AccountService;
import com.springday3aoptransaction.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    //    自动注入logService日志服务
    @Autowired
    private LogService logService;

    @Override
    public List<Account> findAll() {
        return accountDao.findAll();
    }

    @Override
    public Account findById(Integer id) {
        return accountDao.findById(id);
    }

    @Override
    public void transfer(String out, String in, Double money) throws IOException {
        try {
            accountDao.outMoney(out, money);
            int i = 1 / 0;
//            if (true) {
//                throw new IOException();
//            }
            accountDao.inMoney(in, money);
        } finally {
//            无论转账是否成功，都记录到日志中
            logService.log(out, in, money);
        }

    }
}
